package com.zhuzi.guicollect.jsl;

import com.zhuzi.guicollect.bean.Favorite;
import com.zhuzi.guicollect.utils.AlertUtils;
import com.zhuzi.guicollect.utils.HttpUtils;
import com.zhuzi.guicollect.utils.UrlConst;
import javafx.geometry.Pos;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.core.io.ClassPathResource;

import java.io.File;
import java.io.IOException;

/**
 * @author MI
 * @ClassName FavoriteResolver.java
 * @createTime 2021年11月02日 17:56:00
 * 收藏解析
 */
@Slf4j
public class FavoriteResolver {

    public static int getFavPage() {
        Document document = Jsoup.parse(HttpUtils.httpGet(UrlConst.shouchang));
        Elements pagEle = document.select(".pagination li");
        if (pagEle.size() > 0) {
            String pageNumStr = pagEle.get(pagEle.size() - 1).getElementsByTag("a").attr("href");
            String digits = StringUtils.getDigits(pageNumStr);
            return Integer.valueOf(digits);
        }
        return 0;
    }

    public static void reslover(String savePath, int number) {
        Document document = Jsoup.parse(HttpUtils.httpGet(UrlConst.shouchang));
        //抓取第一页
        get(document, savePath);
        //分页
        Elements pagEle = document.select(".pagination li");
        if (pagEle.size() > 0) {
            String pageNumStr = pagEle.get(pagEle.size() - 1).getElementsByTag("a").attr("href");
            String digits = StringUtils.getDigits(pageNumStr);
            for (int i = 2; i <= Math.min(Integer.valueOf(digits), number); i++) {
                String bodyStr = HttpUtils.httpGet(String.format(UrlConst.shoucangPage, "", i));
                document = Jsoup.parse(bodyStr);
                get(document, savePath);
                log.info("采集收藏-第{}页码", i);
                AlertUtils.showNotifications("采集中", "采集第" + i + "页面", 5, Pos.BOTTOM_RIGHT);
            }
        }
        AlertUtils.showNotifications("采集完成", "", 3, Pos.BOTTOM_RIGHT);
    }

    private static void get(Document document, String savePath) {
        Elements favorItem = document.select(".aw-my-favorite .aw-item");
        if (favorItem.size() == 0) {
            return;
        }
        favorItem.forEach(item -> {
            Elements favTitle = item.select("h4>a");
            String href = favTitle.attr("href");
            String title = favTitle.text();
            Favorite favorite = new Favorite(href, title);
            String delMsg = GetPage.isDeled(favorite.getUrl());
            if (StringUtils.isBlank(delMsg)) {
                AlertUtils.showNotifications("采集中", title, 5, Pos.BOTTOM_RIGHT);
                GetPage.get(favorite.getUrl(), savePath + "/favorite", true, false);
            }
        });
    }

    public static void main(String[] args) throws IOException {
        File file = new ClassPathResource("收藏.html").getFile();
        String s = FileUtils.readFileToString(file);
        // reslover(s, "d:/pic", 3);
        Document document = Jsoup.parse(s);
        Elements select = document.select(".pagination");
        Elements pagEle = document.select(".pagination li");
        System.out.println(pagEle);
    }
}
